package com.kaizzi.data.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Kzen
 * @Description 字符串类
 * @date 2020/9/11
 */
public class StringUtil extends StringUtils {

    /**
     * @param data Object
     * @return 00,000.00
     */
    public static String decimalFormat(Object data) {
        DecimalFormat df = new DecimalFormat("#,###.##");
        return df.format(data);
    }
    /**
     * @param data Object
     * @return 00,000.0
     */
    public static String decimalFormat2(Object data) {
        DecimalFormat df = new DecimalFormat("#,###.#");
        return df.format(data);
    }

    /**
     * @param data Object
     * @return 00,000
     */
    public static String pointFormat(Object data) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(data);
    }

    /**
     * @param str 是否字符型
     * @return true or false
     */
//    public static boolean isInteger(String str) {
//        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
//        return pattern.matcher(str).matches();
//    }


    /**
     * 判断是否半数以上为数字
     * @param content
     * @return
     */
    public static boolean hasDigitAll(String content){
        boolean flag = false;
        if (isDigit(content)){
            return true;
        }
        if (hasDigit(content)){
            String tempStr = content.replaceAll("[\\d]+", "");
            if ((((double)tempStr.length()/content.length())) < 0.6){
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 是否包含数字
     * @param strNum
     * @return
     */
    public static boolean isDigit(String strNum) {
        return strNum.matches("[0-9]{1,}");
    }

    public static boolean hasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

    //判断字符串是不是以数字开头
    public static boolean startNum(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str.charAt(0)+"");
        return isNum.matches();
    }

    //字符串转double
    public static double doubleFormat(String value){
        if (value == null){
            return 0;
        }
        return Double.parseDouble(value.replace(",",""));
    }

    //字符串转double
    public static double doubleFormat2(String value){
        if (value == null){
            return 0;
        }
        return Double.parseDouble(value.replace(",","").replace("%",""));
    }


}